//
// Created by twetec on 25-8-16.
//

#include "../include/network_prober/backup_test/handle_telnet_backup_test.h"

#include <boost/json.hpp>

#include <gotelnet/gotelnet.h>

namespace network_prober::backup_test
{
void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, TELNETBackupTestResultIPv4 const &c)
{
    auto& ja = jv.emplace_object();

    ja["interface"] = c.interface;
    ja["target"] = c.target;
    ja["port"] = c.port;
    ja["backupData"] = c.backupData;
}

std::ostream& operator<<(std::ostream& out, const TELNETBackupTestResultIPv4& result)
{
    out
        << "interface: " << result.interface
        << " & target: " << result.target
        << " & backupData: " << result.backupData;

    return out;
}

HandleTELNETBackupTestIPv4::HandleTELNETBackupTestIPv4(TELNETBackupTestParamsIPv4&& testParams)
: m_interface(testParams.interface), m_target(testParams.target), m_port(testParams.port), m_username(testParams.username),
m_password(testParams.password), m_privilegedPassword(testParams.privilegedPassword), m_command(testParams.command)
{

}

void HandleTELNETBackupTestIPv4::setOnTestedCallback(std::function<void(TELNETBackupTestResultIPv4 &&)> callback)
{
    m_onTestedCallback = std::move(callback);
}

void HandleTELNETBackupTestIPv4::test()
{
    // 指向当前对象的shared_ptr
    auto self{shared_from_this()};

    // 执行备份任务
    m_onTestedCallbackFuture = std::async(std::launch::async, [this, self]() {
        // 备份结果对象
        TELNETBackupTestResultIPv4 testResult {
            .interface = m_interface,
            .target = m_target,
            .port = m_port,
            .backupData = ""
        };

        // 测试备份内容
        const char* backupData = TelnetGetConfig(
            const_cast<char*>(m_target.c_str()),
            const_cast<char*>(m_username.c_str()),
            const_cast<char*>(m_password.c_str()),
            const_cast<char*>(m_privilegedPassword.c_str()),
            const_cast<char*>(m_command.c_str()),
            const_cast<char*>(m_interface.c_str()),
            m_port,
            60  // 超时时间 秒
        );

        // 如果内容为null，表示测试失败
        if (backupData == nullptr) {
            // 设置备份内容
            testResult.backupData = "";
            // 回调上报
            m_onTestedCallback(std::move(testResult));
            // 结束执行
            return;
        }

        // 如果测试成功，则上报内容
        // 设置备份内容
        testResult.backupData = backupData;
        // 回调上报
        m_onTestedCallback(std::move(testResult));

        // 任务结束后，自动回收当前的self资源
    });
}

HandleTELNETBackupTestIPv4::~HandleTELNETBackupTestIPv4()
{

}

void tag_invoke(boost::json::value_from_tag, boost::json::value &jv, TELNETBackupTestResultIPv6 const &c)
{
    auto& ja = jv.emplace_object();

    ja["interface"] = c.interface;
    ja["target"] = c.target;
    ja["port"] = c.port;
    ja["backupData"] = c.backupData;
}

HandleTELNETBackupTestIPv6::HandleTELNETBackupTestIPv6(TELNETBackupTestParamsIPv6&& testParams)
: m_interface(testParams.interface), m_target(testParams.target), m_port(testParams.port), m_username(testParams.username),
m_password(testParams.password), m_privilegedPassword(testParams.privilegedPassword), m_command(testParams.command)
{

}

void HandleTELNETBackupTestIPv6::setOnTestedCallback(std::function<void(TELNETBackupTestResultIPv6 &&)> callback)
{
    m_onTestedCallback = std::move(callback);
}

void HandleTELNETBackupTestIPv6::test()
{
    // 指向当前对象的shared_ptr
    auto self{shared_from_this()};

    // 执行备份任务
    m_onTestedCallbackFuture = std::async(std::launch::async, [this, self]() {
        // 备份结果对象
        TELNETBackupTestResultIPv6 testResult {
            .interface = m_interface,
            .target = m_target,
            .port = m_port,
            .backupData = ""
        };

        // 测试备份内容
        const char* backupData = TelnetGetConfig(
            const_cast<char*>(m_target.c_str()),
            const_cast<char*>(m_username.c_str()),
            const_cast<char*>(m_password.c_str()),
            const_cast<char*>(m_privilegedPassword.c_str()),
            const_cast<char*>(m_command.c_str()),
            const_cast<char*>(m_interface.c_str()),
            m_port,
            60  // 超时时间：秒
        );

        // 如果内容为null，表示测试失败
        if (backupData == nullptr) {
            // 设置备份内容
            testResult.backupData = "";
            // 回调上报
            m_onTestedCallback(std::move(testResult));
            // 结束执行
            return;
        }

        // 如果测试成功，则上报内容
        // 设置备份内容
        testResult.backupData = backupData;
        // 回调上报
        m_onTestedCallback(std::move(testResult));

        // 任务结束后，自动回收当前的self资源
    });
}

HandleTELNETBackupTestIPv6::~HandleTELNETBackupTestIPv6()
{

}
}